home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Plus / Multimedia Plus with ClearVue Version 10-94 (Knowledge Media Inc.).ISO / win3x / vid_savr / vidsaver / vidsaver.c < prev    next >
C/C++ Source or Header  |  1993-02-24  |  26KB  |  791 lines

  1. /*  VIDSAVER.C
  2.  *
  3.  *   VIDSAVER is a sample screen saver application. It moves a
  4.  *   playing .AVI file around the screen.  It provides a variety of
  5.  *   options for controlling movement and sound.
  6.  *
  7.  *   (C) Copyright Ron Wodaski 1992-1993.  All rights reserved.
  8.  *
  9.  *   This application was written and compiled using Microsoft C/C++
  10.  *   version 7.0.
  11.  * 
  12.  *   You have a royalty-free right to use, modify, reproduce and 
  13.  *   distribute the sample files (and/or any modified version) in 
  14.  *   any way you find useful, provided that you agree that 
  15.  *   the author, Ron Wodaski has no warranty obligations or liability 
  16.  *   for any sample application files which are modified. 
  17.  */
  18.  
  19. #include <windows.h> 
  20. #include <mmsystem.h>
  21. #include "vidsaver.h"
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <time.h>
  26.  
  27. /* Global used by SCRNSAVE.LIB. Required for all screen savers.
  28.  */
  29. char szAppName[40];
  30.  
  31.  
  32. /* Globals specific to VIDSAVER.
  33.  */
  34. char szDIBName[] = "VideoDIB";
  35. char szSpeedName[] = "Speed";
  36. char szDelayName[] = "Delay between file play";
  37. char szSDelayName[] = "Delay between moves";
  38. char szMagnifyName[] = "Magnification";
  39. char szChangeName[] = "Change frequency";
  40. char szBlankitName[] = "Blank Screen";
  41. char szMuteName[]= "Mute Audio";
  42. char szRepeatName[]= "Repeat Play";
  43. char szPlayFileName[]= "Filename to Play";
  44. char szName[]="Bounce a Video";
  45. char szSoundName[] = "Sound";
  46.  
  47. /* Externals defined in SCRNSAVE.LIB. Required for all screen savers.
  48.  */
  49. HINSTANCE _cdecl hMainInstance;
  50. HWND _cdecl hMainWindow;
  51. char _cdecl szName[TITLEBARNAMELEN];
  52. char _cdecl szIsPassword[22];
  53. char _cdecl szIniFile[MAXFILELEN];
  54. char _cdecl szScreenSaver[22];
  55. char _cdecl szPassword[16];
  56. char _cdecl szDifferentPW[BUFFLEN];
  57. char _cdecl szChangePW[30];
  58. char _cdecl szBadOldPW[BUFFLEN];
  59. char _cdecl szHelpFile[MAXFILELEN];
  60. char _cdecl szNoHelpMemory[BUFFLEN];
  61. UINT _cdecl MyHelpMessage;
  62. HOOKPROC _cdecl fpMessageFilter;
  63.  
  64. BOOL bPassword;       // password protected?
  65.  
  66. WORD wMaxSpeed=25;                    // Maximum # pixels per window move
  67. int ChangeFreq=100;                    // Determines how often change num pixels
  68. char szReturnString[STRLEN];        // buffer for messages from mciSendString
  69. char szWindowHandle[16];            // buffer for string of window handle
  70. char szCommandString[STRLEN];        // buffer for MCI command strings
  71. char szProgName[]="VideoSaver";    // Program name
  72.  
  73. char szFileToPlay[STRLEN];     // Buffer for name of file to play.
  74. char szWinTitle[STRLEN];     // Buffer for playback window caption
  75. char szPlayWidth[8];             // Buffer for playback window width
  76. char szPlayHeight[8];         // Buffer for playback window height
  77. long  wPlayWidth;                 // Playback window width
  78. long  wPlayHeight;             // Playback window height
  79. WORD wMagnification=200;     // Magnification factor
  80. BOOL bMuteAudio=0;             // Toggle for muting audio
  81. BOOL bRepeatPlay=0;             // Toggle for Continuous repeat
  82. BOOL bBlankScreen=1;             // Toggle for blanking screen
  83. int wAdjWidth, wAdjHeight;     // Difference between window/client area sizes
  84. int wTop=0, wLeft=0;             // X,Y coordinates for playback window create
  85.  
  86. HWND temp;
  87. HWND hScrWnd;                    // Handle for playback window
  88. WNDCLASS wcScrApp;            // Class for playback window
  89. FILE *FileOpen;                // File open pointer for fopen call
  90. RECT lpScreen;                    // For screen rectangle
  91. RECT lpWindowSize;            // For playback window rectangle
  92. RECT lpClientSize;            // For playback window client area rectangle
  93. long wWidth, wHeight;        // Initial size of playback window
  94.  
  95. WORD wTimerShort;          // timer id for interval on window move
  96. int ShortTime=100;            // default length of timer
  97. WORD wTimerLong;           // timer id for interval on file play
  98. int DelayTime=250;            // default length of timer
  99. UINT PlayingTime=0;                // Length of AVI file in ms
  100. HBITMAP hbmImage;                   // image handle
  101. HANDLE hresWave;                    // handle to sound resource
  102. LPSTR lpWave;                       // pointer to wave resource
  103.  
  104. /* Toggle for repainting during calls to MoveWindow() */
  105. static BOOL RepaintToggle=FALSE;
  106.  
  107.  
  108. /* ScreenSaverProc - Main entry point for screen saver messages.
  109.  *  This function is required for all screen savers.
  110.  *
  111.  * Params:  Standard window message handler parameters.
  112.  *
  113.  * Return:  The return value depends on the message.
  114.  *
  115.  *  Note that all messages go to the DefScreenSaverProc(), except
  116.  *  for ones we process.
  117.  */
  118. LONG FAR PASCAL ScreenSaverProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  119. {
  120.     RECT rc;                          // Rectangle for erasing background
  121.  
  122.     switch (msg)
  123.     {
  124.         case WM_CREATE:
  125.         {
  126.             /* Load the strings from the STRINGTABLE */
  127.             GetIniEntries();
  128.  
  129.             /* Load the initial bounce settings. */
  130.             GetIniSettings();
  131.  
  132.                 srand((unsigned)time(NULL));  // Seed random # generation.
  133.  
  134.                 /* Make default window size one-fourth screen size.  */
  135.                 GetWindowRect(GetDesktopWindow(),&lpScreen);
  136.                 wWidth  = (long)(lpScreen.right-lpScreen.left)/4;
  137.                 wHeight = (long)(lpScreen.bottom-lpScreen.top)/4;
  138.  
  139.                 /* Create window class for playback. */
  140.                 wcScrApp.lpszClassName=szProgName;
  141.                 wcScrApp.hInstance    =hMainInstance;
  142.                 wcScrApp.lpfnWndProc  =WndProc;
  143.                 wcScrApp.hCursor      =LoadCursor(NULL,IDC_ARROW);
  144.                 wcScrApp.hIcon        =NULL;
  145.                 wcScrApp.lpszMenuName =NULL;
  146.                 wcScrApp.hbrBackground=CreateSolidBrush(RGB(255,255,255));
  147.                 wcScrApp.style        =CS_HREDRAW|CS_VREDRAW;
  148.                 wcScrApp.cbClsExtra   =0;
  149.                 wcScrApp.cbWndExtra   =0;
  150.                 if (!RegisterClass (&wcScrApp))
  151.                    return FALSE;
  152.  
  153.                 /* Create playback window. */
  154.                 hScrWnd=CreateWindow(szProgName,"VidSaver 0.1c",
  155.                        (WS_POPUP), wLeft, wTop,
  156.                        (int)wWidth,(int)wHeight, hWnd,(HMENU)NULL,
  157.                        (HANDLE)hMainInstance,(LPSTR)NULL);
  158.  
  159.                 /* Get sizes of Window & Client area for later calculations. */
  160.                 GetWindowRect(hScrWnd,&lpWindowSize);
  161.                 GetClientRect(hScrWnd,&lpClientSize);
  162.  
  163.                 /* Setup for playback. */
  164.                 PlayingTime = SetupPlayback(hWnd, szFileToPlay);
  165.  
  166.                 /* Set a timer for interval between playback window moves. */
  167.              wTimerShort=SetTimer(hWnd, ID_TIMER, ShortTime, NULL);
  168.  
  169.                 if (FileOpen)
  170.                     {
  171.                  MovePlayerRandom(hScrWnd);
  172.                     ShowWindow(hScrWnd,SW_SHOWNORMAL);
  173.  
  174.                     /* Set a timer for the length of the file plus delay time * 10. */
  175.                  wTimerLong=SetTimer(hScrWnd,ID_TIMER,PlayingTime+DelayTime*10, NULL);
  176.  
  177.                     /* Play the AVI file! */
  178.                  PlayFile(hScrWnd);
  179.                     }
  180.              return 0L;
  181.         }
  182.  
  183.         case WM_TIMER:
  184.  
  185.                 /* Short timer went off; move window to new location. */
  186.                 if (FileOpen)
  187.                   MovePlayer(hScrWnd);
  188.                 else
  189.                     MovePlayer(temp);
  190.               return 0L;
  191.  
  192.         case WM_DESTROY:
  193.  
  194.                 /* Clean up; we're out of here. */
  195.                 if (FileOpen)
  196.                     mciExecute("close AVIFile"); 
  197.                 /* Kill timer. */
  198.               if(wTimerShort) KillTimer(hWnd,ID_TIMER);
  199.                 /* Destroy window. */
  200.                 if (hScrWnd) DestroyWindow(hScrWnd);
  201.                 if (temp) DestroyWindow(temp);
  202.  
  203.             if( hbmImage ) DeleteObject(hbmImage);
  204.             if( lpWave )   UnlockResource(hresWave);
  205.             if( hresWave ) FreeResource(hresWave);
  206.             sndPlaySound(NULL, 0);
  207.             break;
  208.  
  209.         case WM_ERASEBKGND:
  210.                 if (bBlankScreen)
  211.                     {
  212.                 GetClientRect(hWnd,&rc);
  213.                 FillRect((HDC)wParam,&rc,(HBRUSH)GetStockObject(BLACK_BRUSH));
  214.                     }
  215.             return 0L;
  216.  
  217.         default:
  218.             break;
  219.         }
  220.     /* If we don't handle message, pass it to default procedure. */
  221.    return DefScreenSaverProc(hWnd, msg, wParam, lParam);
  222. }
  223.  
  224. /* RegisterDialogClasses -- Entry point for registering window
  225.  * classes required by configuration dialog box.
  226.  *
  227.  * Params:  hWnd -- Handle to window
  228.  *
  229.  * Return:  None
  230.  */
  231. BOOL RegisterDialogClasses(HINSTANCE hInst)
  232. {
  233.     return TRUE;
  234. }
  235.  
  236.  
  237. /* ScreenSaverConfigureDialog -- Dialog box function for configuration
  238.  * dialog.
  239.  *
  240.  * Params:  hWnd -- Handle to window
  241.  *
  242.  * Return:  None
  243.  */
  244. BOOL FAR PASCAL ScreenSaverConfigureDialog(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam)
  245. {
  246.   static HWND hIDOK;
  247.   static HWND hSetPassword;
  248.  
  249.     switch (msg)
  250.     {
  251.         case WM_INITDIALOG:
  252.             GetIniEntries();        // Load strings from table.
  253.             GetIniSettings();        // Read from CONTROL.INI
  254.  
  255.                 /* Set values in dialog box. */
  256.             SetDlgItemInt(hDlg, ID_SPEED,   wMaxSpeed,      FALSE);
  257.             SetDlgItemInt(hDlg, ID_BDELAY,  DelayTime,      TRUE );
  258.             SetDlgItemInt(hDlg, ID_MDELAY,  ShortTime,      TRUE );
  259.             SetDlgItemInt(hDlg, ID_MAGNIFY, wMagnification, TRUE );
  260.             SetDlgItemInt(hDlg, ID_CLEVEL,  ChangeFreq,     TRUE );
  261.                SetDlgItemText(hDlg,ID_FNAME,    szFileToPlay);
  262.             SendDlgItemMessage(hDlg, ID_BLANKIT, BM_SETCHECK, bBlankScreen, NULL);
  263.             SendDlgItemMessage(hDlg, ID_MUTE,    BM_SETCHECK, bMuteAudio,   NULL);
  264.             SendDlgItemMessage(hDlg, ID_CONT,    BM_SETCHECK, bRepeatPlay,  NULL);
  265.             SendDlgItemMessage(hDlg, ID_PASSWORDPROTECTED, BM_SETCHECK,
  266.                 bPassword, NULL);
  267.             hSetPassword=GetDlgItem(hDlg, ID_SETPASSWORD);
  268.             EnableWindow(hSetPassword, bPassword);
  269.             hIDOK=GetDlgItem(hDlg, IDOK);
  270.             return TRUE;
  271.  
  272.         case WM_COMMAND:
  273.             switch (wParam)
  274.             {
  275.                 case IDOK:
  276.                           /* User clicked OK button; put values into variables. */
  277.                     wMaxSpeed      = GetDlgItemInt(hDlg, ID_SPEED,   NULL, FALSE );
  278.                     DelayTime      = GetDlgItemInt(hDlg, ID_BDELAY,  NULL, TRUE  );
  279.                     ShortTime      = GetDlgItemInt(hDlg, ID_MDELAY,  NULL, TRUE  );
  280.                     wMagnification = GetDlgItemInt(hDlg, ID_MAGNIFY, NULL, TRUE  );
  281.                     ChangeFreq     = GetDlgItemInt(hDlg, ID_CLEVEL,  NULL, TRUE  );
  282.                     GetDlgItemText(hDlg,ID_FNAME,    szFileToPlay,   STRLEN);
  283.                     bBlankScreen   = IsDlgButtonChecked(hDlg, ID_BLANKIT         );
  284.                     bRepeatPlay    = IsDlgButtonChecked(hDlg, ID_CONT            );
  285.                     bMuteAudio     = IsDlgButtonChecked(hDlg, ID_MUTE            );
  286.                     bPassword      = IsDlgButtonChecked(hDlg, ID_PASSWORDPROTECTED);
  287.  
  288.                           /* Check the values we just loaded! */
  289.                           VerifyIniSettings();
  290.  
  291.                           /* Put values into CONTROL.INI. */
  292.                     WriteProfileInt(szAppName, szSpeedName,   wMaxSpeed      );
  293.                     WriteProfileInt(szAppName, szDelayName,   DelayTime      );
  294.                     WriteProfileInt(szAppName, szSDelayName,  ShortTime      );
  295.                     WriteProfileInt(szAppName, szMagnifyName, wMagnification );
  296.                     WriteProfileInt(szAppName, szChangeName,  ChangeFreq     );
  297.  
  298.                           WritePrivateProfileString(szAppName, szPlayFileName, szFileToPlay,
  299.                                  szIniFile);
  300.  
  301.                     WriteProfileInt(szAppName, szBlankitName, bBlankScreen   );
  302.                     WriteProfileInt(szAppName, szMuteName,    bMuteAudio     );
  303.                     WriteProfileInt(szAppName, szRepeatName,  bRepeatPlay     );
  304.                     WriteProfileInt(szAppName, szIsPassword,  bPassword      );
  305.  
  306.                           /* Close dialog box. */
  307.                     EndDialog(hDlg, TRUE);
  308.                     return TRUE;
  309.  
  310.                 case IDCANCEL:
  311.                           /* Cancelling dialog box.  Save nothing and close. */
  312.                     EndDialog(hDlg, FALSE);
  313.                     return TRUE;
  314.  
  315.                 case ID_SETPASSWORD:
  316.                 {
  317.                     FARPROC fpDialog;
  318.                           /* Get and set password. */
  319.                       if((fpDialog = MakeProcInstance(DlgChangePassword,hMainInstance)) == NULL)
  320.                         return FALSE;
  321.                     DialogBox(hMainInstance, MAKEINTRESOURCE(DLG_CHANGEPASSWORD), 
  322.                               hDlg, fpDialog);
  323.                     FreeProcInstance(fpDialog);
  324.                     SendMessage(hDlg, WM_NEXTDLGCTL, hIDOK, 1l);
  325.                     break;
  326.                 }
  327.  
  328.                 case ID_PASSWORDPROTECTED:
  329.  
  330.                           /* Password protection enabled. */
  331.                     bPassword ^= 1;
  332.                     CheckDlgButton(hDlg, wParam, bPassword);
  333.                     EnableWindow(hSetPassword, bPassword);
  334.                     break;
  335.  
  336.                 case ID_HELP:
  337. DoHelp:
  338. #if 0
  339.                     bHelpActive=WinHelp(hDlg, szHelpFile, HELP_CONTEXT, IDH_DLG_BOUNCER);
  340.                     if (!bHelpActive)
  341.                         MessageBox(hDlg, szNoHelpMemory, szName, MB_OK);
  342. #else
  343.                     MessageBox(hDlg, "Insert your call to WinHelp() here.",
  344.                         szName, MB_OK);
  345. #endif
  346.                     break;
  347.             }
  348.             break;
  349.         default:
  350.             if (msg==MyHelpMessage)     // Context sensitive help msg.
  351.                 goto DoHelp;
  352.     }
  353.     return FALSE;
  354. }
  355.  
  356.  
  357. /* GetIniSettings -- Get initial bounce settings from WIN.INI
  358.  *
  359.  * Params:  hWnd -- Handle to window
  360.  *
  361.  * Return:  None
  362.  */
  363. static void GetIniSettings()
  364. {
  365.      /* Load initialization settings from CONTROL.INI. */
  366.     wMaxSpeed = GetPrivateProfileInt(szAppName,szSpeedName,DEF_SPEED,szIniFile);
  367.     DelayTime = GetPrivateProfileInt(szAppName,szDelayName,DEF_INIT_BDELAY,szIniFile);
  368.     ShortTime = GetPrivateProfileInt(szAppName,szSDelayName,DEF_INIT_MDELAY,szIniFile);
  369.     wMagnification = GetPrivateProfileInt(szAppName,szMagnifyName,DEF_INIT_MAGNIFY,
  370.                                      szIniFile);
  371.     ChangeFreq = GetPrivateProfileInt(szAppName,szChangeName,DEF_INIT_CLEVEL,szIniFile);
  372.     GetPrivateProfileString(szAppName, szPlayFileName, DEF_FNAME,szFileToPlay,  
  373.                                     STRLEN, szIniFile);
  374.     bBlankScreen = GetPrivateProfileInt(szAppName, szBlankitName,  DEF_BLANKIT,szIniFile);
  375.     bMuteAudio = GetPrivateProfileInt(szAppName, szMuteName,     DEF_MUTE,szIniFile);
  376.     bRepeatPlay = GetPrivateProfileInt(szAppName, szRepeatName,   DEF_CONT,szIniFile);
  377.     bPassword = GetPrivateProfileInt(szAppName, szIsPassword,   FALSE, szIniFile);
  378.  
  379.     /* Check the values we just loaded! */
  380.     VerifyIniSettings();
  381. }
  382.  
  383. /* WriteProfileInt - Write an unsigned integer value to CONTROL.INI.
  384.  *
  385.  * Params:  name - szSection - [section] name in .INI file
  386.  *                 szKey     - key= in .INI file
  387.  *                 i         - value for key above
  388.  *
  389.  * Return:  None
  390.  */
  391. static void WriteProfileInt(LPSTR szSection, LPSTR szKey, int i) 
  392. {
  393.     char achBuf[40];
  394.  
  395.     /* write out as unsigned because GetPrivateProfileInt() can't
  396.      * cope with signed values!
  397.      */
  398.     wsprintf(achBuf, "%u", i);
  399.     WritePrivateProfileString(szSection, szKey, achBuf, szIniFile);
  400. }
  401.  
  402. void GetIniEntries(void)
  403. {
  404.   //Load Common Strings from stringtable...
  405.   LoadString(hMainInstance, idsIsPassword,   szIsPassword,   22        );
  406.   LoadString(hMainInstance, idsIniFile,      szIniFile,      MAXFILELEN);
  407.   LoadString(hMainInstance, idsScreenSaver,  szScreenSaver,  22        );
  408.   LoadString(hMainInstance, idsPassword,     szPassword,     16        );
  409.   LoadString(hMainInstance, idsDifferentPW,  szDifferentPW,  BUFFLEN   );
  410.   LoadString(hMainInstance, idsChangePW,     szChangePW,     30        );
  411.   LoadString(hMainInstance, idsBadOldPW,     szBadOldPW,     255       );
  412.   LoadString(hMainInstance, idsHelpFile,     szHelpFile,     MAXFILELEN);
  413.   LoadString(hMainInstance, idsNoHelpMemory, szNoHelpMemory, BUFFLEN   );
  414. }
  415.  
  416. /* PlayFile - Play the AVI file.
  417.  *
  418.  * Params:  hWnd - Window used for playback. 
  419.  *
  420.  * Return:  None
  421.  */
  422. static void PlayFile(HWND hWnd)
  423. {
  424.     /* See if the file is open for playing. */
  425.     if (FileOpen)
  426.         {
  427.         /* Go to first frame in file.
  428.         mciExecute("seek AVIFile to 0"); 
  429.  
  430.         /* Construct a "put" command string for window and execute it. */
  431.         strcpy(szReturnString,"put AVIFIle window at 0 0 ");
  432.         strcat(szReturnString,szPlayWidth);
  433.         strcat(szReturnString," ");
  434.         strcat(szReturnString,szPlayHeight);
  435.         mciExecute(szReturnString); 
  436.  
  437.         /* Construct a "put" command string for destination and execute it. */
  438.         strcpy(szReturnString,"put AVIFIle destination at 0 0 ");
  439.         strcat(szReturnString,szPlayWidth);
  440.         strcat(szReturnString," ");
  441.         strcat(szReturnString,szPlayHeight);
  442.         mciExecute(szReturnString); 
  443.  
  444.         /* Play the file, either continuously or one time. */
  445.         if (bRepeatPlay)
  446.             mciExecute("play AVIFile window repeat"); 
  447.         else
  448.             mciExecute("play AVIFile from 0"); 
  449.         }
  450. }
  451.  
  452. /* MovePlayer - Move player (called when short timer goes off).
  453.  *
  454.  * Params:  hWnd - Window used for playback. 
  455.  *
  456.  * Return:  None
  457.  */
  458.  
  459. /* Default values for horizontal and vertical increments for moves. */
  460. static UINT Vinc=1;
  461. static UINT Hinc=1;
  462.  
  463. static void MovePlayer(HWND hWindow)
  464. {
  465.     int Temp;            // Holder for temporary values.
  466.  
  467.     /* Toggles for defining current direction. */
  468.     static BOOL Horizontal=TRUE, Vertical=TRUE;
  469.  
  470.     /* Get a random number. */
  471.     Temp = rand();
  472.     /* If mod zero, time to change direction. */
  473.     if (Temp % ChangeFreq == 0)
  474.         {
  475.         /* Change vectors. */
  476.         Horizontal = !Horizontal;
  477.         Temp = rand();
  478.         Hinc = (Temp % wMaxSpeed)+1;
  479.         }
  480.     /* Get a random number. */
  481.     Temp = rand();
  482.     /* If mod zero, time to change direction. */
  483.     if (Temp % ChangeFreq == 0)
  484.         {
  485.         /* Change vectors. */
  486.         Vertical = !Vertical;
  487.         Temp = rand();
  488.         Vinc = (Temp % wMaxSpeed)+1;
  489.         }
  490.  
  491.     /* Now that we have established direction and amount, apply them! */
  492.     if (Vertical)
  493.         wTop += Vinc;
  494.     else
  495.         wTop -= Vinc;
  496.     if (Horizontal)
  497.         wLeft += Hinc;
  498.     else
  499.         wLeft -= Hinc;
  500.  
  501.     /* Make sure we haven't wandered off of the screen. */
  502.     if (wLeft <= 0)
  503.         {
  504.         wLeft = 1;
  505.         Horizontal = !Horizontal;
  506.         }
  507.     if (wTop <= 0)
  508.         {
  509.         wTop = 1;
  510.         Vertical = !Vertical;
  511.         }
  512.  
  513.     if (wTop  > (lpScreen.bottom-((int)wPlayHeight+wAdjHeight)))
  514.         {
  515.         wTop = lpScreen.bottom-((int)wPlayHeight+wAdjHeight);
  516.         Vertical = !Vertical;
  517.         }
  518.     if (wLeft > (lpScreen.right-((int)wPlayWidth+wAdjWidth)))
  519.         {
  520.         Horizontal = !Horizontal;
  521.         wLeft = lpScreen.right-((int)wPlayWidth+wAdjWidth);
  522.         }
  523.  
  524.     /* Are we working with a blank screen or not? */
  525.     if (bBlankScreen)
  526.         SetWindowPos(hWindow,HWND_TOP,wLeft,wTop,(int)wPlayWidth+wAdjWidth,(int)wPlayHeight+
  527.                              wAdjHeight,SWP_NOSIZE|SWP_NOZORDER);
  528.     else
  529.         MoveWindow(hWindow,wLeft,wTop,(int)wPlayWidth+wAdjWidth,(int)wPlayHeight+wAdjHeight, 
  530.                                 RepaintToggle);
  531. }
  532.  
  533. /* MovePlayerRandom - Move player to a random screen location.
  534.  *
  535.  * Params:  hWnd - Window used for playback. 
  536.  *
  537.  * Return:  None
  538.  */
  539. static void MovePlayerRandom(HWND hWindow)
  540. {
  541.     /* Get new coordinates. */
  542.     wTop = rand();
  543.     wLeft = rand();
  544.  
  545.     /* Check to see if new coordinates are within screen; do again if not. */
  546.     while (wTop  > (lpScreen.bottom-(int)wPlayHeight-45))
  547.         wTop = rand();
  548.     while (wLeft > (lpScreen.right-(int)wPlayWidth-25)) 
  549.         wLeft = rand();
  550.  
  551.     /* Move Window; method depends on whether we are blanking screen. */
  552.     if (bBlankScreen)
  553.         SetWindowPos(hWindow,HWND_TOP,wLeft,wTop,(int)wPlayWidth+wAdjWidth,(int)wPlayHeight+
  554.                 wAdjHeight,SWP_NOZORDER);
  555.     else
  556.         MoveWindow(hWindow,wLeft,wTop,(int)wPlayWidth+wAdjWidth,(int)wPlayHeight+wAdjHeight,
  557.                 RepaintToggle);
  558.  
  559. }
  560.  
  561. /* WndProc - Default procedure for playback window.
  562.  *
  563.  */
  564. LONG FAR PASCAL WndProc(HWND hWindow,UINT messg,
  565.                         WPARAM wParam,LPARAM lParam)
  566. {
  567.     PAINTSTRUCT ps;
  568.     HDC hdc;
  569.     TEXTMETRIC tm;
  570.  
  571.     char szMessage[]="File not found:";
  572.     static short CharHeight;
  573.  
  574.     switch (messg)
  575.     {
  576.         case WM_CREATE:
  577.             hdc = GetDC(hWindow);
  578.             GetTextMetrics(hdc, &tm);
  579.             CharHeight = tm.tmHeight;
  580.             ReleaseDC(hWindow, hdc);
  581.             break;
  582.  
  583.         case WM_PAINT:
  584.             hdc=BeginPaint(hWindow,&ps);
  585.  
  586.             // Put anything you want to display here.
  587.  
  588.             if (!FileOpen)
  589.                 {
  590.                 PlaceBitmap(hWindow);
  591.                 PlaySoundResource();
  592.                 
  593.                 SetTextAlign(hdc,TA_CENTER);
  594.  
  595.                 TextOut(hdc, 160, 187, szMessage, strlen(szMessage));
  596.                 TextOut(hdc, 160, 187+CharHeight, szFileToPlay, strlen(szFileToPlay));
  597.                 }
  598.  
  599.             ValidateRect(hWindow,NULL);
  600.             EndPaint(hWindow,&ps);
  601.             break;
  602.  
  603.         case WM_TIMER:
  604.             if (!bRepeatPlay)
  605.                 /* Timer went off; play AVI file again. */
  606.                 PlayFile(hScrWnd);
  607.           return 0L;
  608.             break;
  609.  
  610.     case WM_DESTROY:
  611.           if(wTimerLong)  KillTimer(hScrWnd,ID_TIMER);
  612.           PostQuitMessage(0);
  613.           break;
  614.     default:
  615.           return(DefWindowProc(hWindow,messg,wParam,lParam));
  616.   }
  617.   return(0L);
  618. }
  619.  
  620. // HDC hDC                            // Handle to our window DC
  621.  
  622. void PlaceBitmap (HWND hWindow)
  623. {
  624.     HDC hDC;
  625.     HDC hMemDC;                         // Handle to a memory DC
  626.     BITMAP bm;                          // Bitmap info
  627.     HBITMAP hbmOld;
  628.  
  629.     hDC = GetDC(hWindow);
  630.     hbmImage = LoadBitmap(hMainInstance, szDIBName);
  631.  
  632.     /* Get bitmap size
  633.      */
  634.     GetObject(hbmImage, sizeof(bm), (LPSTR)&bm);
  635.  
  636.     hMemDC = CreateCompatibleDC(hDC);
  637.     hbmOld = SelectObject(hMemDC, hbmImage);
  638.  
  639.     if(hbmOld)
  640.         {
  641.         /* Blit the image in the new position
  642.          */
  643.         BitBlt(hDC,                        // dest DC
  644.             0,0,                        // dest origin
  645.             bm.bmWidth,bm.bmHeight,     // dest extents
  646.             hMemDC,                     // src DC
  647.             0,0,                        // src origin
  648.             SRCCOPY );                  // ROP code
  649.  
  650.         SelectObject(hMemDC, hbmOld);
  651.         DeleteDC(hMemDC);
  652.        ReleaseDC(hWindow, hDC);
  653.         }
  654.  
  655. }
  656.  
  657. void PlaySoundResource(void)
  658. {
  659.    /* Load, lock, and play the sound resource
  660.     */
  661.    HANDLE hResInfo;
  662.  
  663.    if( hResInfo = FindResource(hMainInstance, "Sound", "WAVE") )
  664.    {
  665.        if( hresWave = LoadResource(hMainInstance, hResInfo) )
  666.        {
  667.            lpWave = LockResource(hresWave);
  668.               if (lpWave)
  669.                   sndPlaySound(lpWave, SND_ASYNC | SND_MEMORY);
  670.        }
  671.    }
  672. }
  673.  
  674. /* SetupPlayback - Set up the Playback window and perform file housekeeping.
  675.  *
  676.  * Params:  AVIFile - name of AVI file to open and set up.
  677.  *
  678.  * Return:  Unsigned integer - Playing time in milliseconds.
  679.  */
  680. UINT SetupPlayback(HWND hWnd, char * AVIFile)
  681. {
  682.     DWORD Result;
  683.     UINT FrameRate, FrameTotal;
  684.     char *tempchar;
  685.     char szMessage[]="  AVI playback file not found:  ";
  686.  
  687.     /* Verify that file exists. */
  688.     FileOpen = fopen(AVIFile, "r");
  689.  
  690.     if (FileOpen)
  691.         {
  692.         fclose(FileOpen);
  693.  
  694.         /* Get string copy of window handle. */
  695.         _itoa(hScrWnd, szWindowHandle, 10);
  696.     
  697.         /* Get file info. */
  698.         strcpy(szCommandString,"open ");
  699.         strcat(szCommandString,AVIFile);
  700.         strcat(szCommandString," style child parent ");
  701.         strcat(szCommandString,szWindowHandle);
  702.         strcat(szCommandString," alias AVIFile");
  703.         mciExecute(szCommandString);
  704.         mciExecute("set AVIFile time format frames");
  705.         if (bMuteAudio)
  706.             mciExecute("setaudio AVIFile off");
  707.         
  708.         /* Get size of frame. */
  709.         Result = mciSendString("where AVIFile window",szReturnString,1024,0);
  710.         tempchar = strtok(szReturnString," ");
  711.         tempchar = strtok(NULL," ");
  712.         tempchar = strtok(NULL," ");
  713.         strcpy(szPlayWidth,tempchar);
  714.         tempchar = strtok(NULL," ");
  715.         strcpy(szPlayHeight,tempchar);
  716.  
  717.         /* Put width and height into integers. */
  718.         wPlayWidth  = atol(szPlayWidth);
  719.         wPlayHeight = atol(szPlayHeight);
  720.  
  721.         /* Apply magnification factor. */
  722.         wPlayWidth  = (wPlayWidth  * wMagnification)/100;
  723.         wPlayHeight = (wPlayHeight * wMagnification)/100;
  724.  
  725.         /* Check to make sure image isn't too big for screen! */
  726.         if (wPlayWidth > (wWidth * 3))
  727.             wPlayWidth = wWidth * 3;
  728.         if (wPlayHeight > wHeight * 3)
  729.             wPlayHeight = wHeight * 3;
  730.  
  731.         /* Put width and hieght back into strings. */
  732.         _ltoa(wPlayWidth,  szPlayWidth, 10);
  733.         _ltoa(wPlayHeight, szPlayHeight, 10);
  734.  
  735.         /* Resize window to fit frame size. */
  736.         wAdjWidth = (lpWindowSize.right-lpWindowSize.left) - (lpClientSize.right-
  737.                 lpClientSize.left);
  738.         wAdjHeight = (lpWindowSize.bottom-lpWindowSize.top) - (lpClientSize.bottom-
  739.                 lpClientSize.top);
  740.  
  741.         mciSendString("status AVIFile length",szReturnString,1024,0);
  742.         FrameTotal = (UINT)atoi(szReturnString);
  743.     
  744.         mciSendString("status AVIFile nominal frame rate",szReturnString,1024,0);
  745.         FrameRate = (UINT)(atoi(szReturnString))/1000;
  746.         return (UINT)(FrameTotal/FrameRate)*1000;
  747.  
  748.         }
  749.     else
  750.         {
  751.  
  752.         wPlayWidth  = 320L;
  753.         wPlayHeight = 240L;
  754.         RepaintToggle = TRUE;
  755.  
  756.         /* Create new playback window as overlapped. */
  757.         temp=CreateWindow(szProgName,"Video Saver Error",
  758.                     (WS_OVERLAPPED), wLeft, wTop,
  759.                     (int)wPlayWidth, (int)wPlayHeight, hWnd, (HMENU)NULL,
  760.                     (HANDLE)hMainInstance, (LPSTR)NULL);
  761.  
  762.         /* Get sizes of Window & Client area for later calculations. */
  763.         GetWindowRect(temp,&lpWindowSize);
  764.         GetClientRect(temp,&lpClientSize);
  765.         wAdjWidth = (lpWindowSize.right-lpWindowSize.left) - (lpClientSize.right-
  766.                 lpClientSize.left);
  767.         wAdjHeight = (lpWindowSize.bottom-lpWindowSize.top) - (lpClientSize.bottom-
  768.                 lpClientSize.top);
  769.  
  770.         /* Setup for playback. */
  771.         MovePlayerRandom(temp);
  772.         ShowWindow(temp,SW_SHOWNORMAL);
  773.         return 1;
  774.         }
  775. }
  776.  
  777. void VerifyIniSettings(void)
  778. {
  779.     /* Make sure that all settings have valid values. */
  780.     if (wMagnification < 10)
  781.         wMagnification = 100;
  782.     if (wMaxSpeed < 1 || wMaxSpeed > 25)
  783.         wMaxSpeed = 5;
  784.     if (ShortTime < 10)
  785.         ShortTime = 10;
  786.     if (DelayTime < 5)
  787.         DelayTime = 5;
  788.     if (ChangeFreq < 5)
  789.         ChangeFreq = 25;
  790. }
  791.